package cn.com.jrr.core.common.mytag;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;

import org.springframework.web.servlet.tags.RequestContextAwareTag;

import cn.com.jrr.core.common.StringUtils;
import cn.com.jrr.core.common.WebKey;
import cn.com.jrr.core.model.power.SysResource;
import cn.com.jrr.core.model.power.SysUser;
import cn.com.jrr.core.service.dictionary.CodeTypeService;
public class TagButtons extends RequestContextAwareTag {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private CodeTypeService codeTypeService;
    //id属性
    private String id;
    //button type
    private String type;
    //button class
    private String cssclass;
    //button style
    private String style;
    //button icon图标
    private String icon;
    //button context
    private String content;
    //onchange 事件
    private String onchange;
    //onclick 事件
    private String onclick;
    //其他属性
    private String others;
    //权限编码
    private String permit;

    //是否有权限
    private boolean hasPermit=false;

    @Override
    protected int doStartTagInternal() throws JspTagException  {
        HttpSession session=pageContext.getSession();
        @SuppressWarnings("unchecked")
        List<SysResource> srs=(List<SysResource>)session.getAttribute(WebKey.MENU);
        for(SysResource sr:srs){
            if("3".equals(sr.getResourceType())){
                if(sr.getPermit().equals(permit)){
                    hasPermit=true;
                    break;
                }
            }
        }
        //hasPermit();
        JspWriter out = pageContext.getOut();
        StringBuffer sb = new StringBuffer() ;
        if(StringUtils.isBlank(id)){
            sb.append("<button id='"+id+"'");
        }
        if(StringUtils.isBlank(type)){
            sb.append(" type='"+type+"'");
        }
        if(StringUtils.isBlank(cssclass)){
            sb.append(" class='"+cssclass+"'");
        }
        if(StringUtils.isBlank(style)){
            //有权限
            if(hasPermit){
                sb.append(" style='"+style+"'");
            }else{//没有权限
                if(style.endsWith(";")){
                    style+="display:none;";
                }else{
                    style+=";display:none;";
                }
                sb.append(style);
            }
        }else{
            //无权限
            if(!hasPermit){
                sb.append(" style='display:none;'");
            }
        }

        if(StringUtils.isBlank(onchange)){
            sb.append(" onchange='"+onchange+"'");
        }
        if(StringUtils.isBlank(onclick)){
            sb.append(" onclick='"+onclick+"'");
        }
        if(StringUtils.isBlank(others)){
            sb.append(" "+others+"'");
        }
        sb.append(">");

        if(StringUtils.isBlank(icon)){
            content="<i class='"+icon+"'></i> "+content;
        }
        if(StringUtils.isBlank(content)){
            sb.append(content+"</button>");
        }
        try {
            out.print(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE; // 表示处理完标签后继续执行以下的JSP网页
        // return SKIP_PAGE; //表示不处理接下来的JSP网页
    }
    private void hasPermit(){

    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getCssclass() {
        return cssclass;
    }
    public void setCssclass(String cssclass) {
        this.cssclass = cssclass;
    }
    public String getStyle() {
        return style;
    }
    public void setStyle(String style) {
        this.style = style;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getOnchange() {
        return onchange;
    }
    public void setOnchange(String onchange) {
        this.onchange = onchange;
    }
    public String getOnclick() {
        return onclick;
    }
    public void setOnclick(String onclick) {
        this.onclick = onclick;
    }
    public String getOthers() {
        return others;
    }
    public void setOthers(String others) {
        this.others = others;
    }
    public String getPermit() {
        return permit;
    }
    public void setPermit(String permit) {
        this.permit = permit;
    }
    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }

}
